//
//  HTuserCount.swift
//  mySinaBlog
//
//  Created by whtapple on 3/24/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit

class HTuserCount: NSObject,NSCoding{
    //用于调用access_token，接口获取授权后的access token
    var access_token: String?
    //过期秒数
    var expires_in: NSTimeInterval = 0 {
        didSet{
            //当kvc给上面赋值时。给这个赋值
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //过期时间
    var expiresDate: NSDate?
    var uid: String?
    init(dict: [String: AnyObject]){
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    //	用户昵称
    var screen_name: String?
    //	用户头像地址（大图），180×180像素
    var avatar_large: String?
    //字典中没有赋值的
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        print("forUndefinedKey:\(key)")
    }
    override var description: String{
        get{
            let keys = ["access_token","expires_in","expiresDate","uid"]
            return dictionaryWithValuesForKeys(keys).description
        }
        
    }
    // MARK: - 归档和解档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")

    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init()
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
}
